<?php
/**
 * 后台_栏目_控制器
 */
class Menu extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model('Admin_menu_mdl','menu');
		$this->load->helper('category');
	}
	public function index(){
		$list = $this->menu->getList();
		$list = Category::unlimitedForLevel($list);

		$temp['list'] = $list;
		$this->showView('admin/system/menu_index',$temp);
	}
	
	private function _showEdit($temp=array()){
		$list = $this->menu->getList();
		$list = Category::unlimitedForLevel($list);
		$temp['list'] = $list;
		$this->showView('admin/system/menu_edit',$temp);
	}
	private function _getEditData(){
		$input = $_REQUEST;
		unset($input['mid']);
		if ( !isset($input['isVisible']) ) {
			$input['isVisible'] = 0;
		}
		return $input;
	}

	public function add(){
		if( empty($_POST) ){
			$temp['title'] = '增加栏目';
			$this->_showEdit($temp);
			return;
		}

		$insert = $this->_getEditData();
		if( empty($insert) ){
			alert('增加栏目失败:数据错误',ADMIN_DIR.'menu/index');
			exit();
		}

		if($this->menu->insert($insert)){
			alert('增加栏目成功',ADMIN_DIR.'menu/index');
		}else{
			alert('增加栏目失败',ADMIN_DIR.'menu/index');
		}

	}

	public function update(){
		$mid = $this->input->get('mid',TRUE);
		if( empty($_POST) ){
			$detail = $this->menu->getOneById($mid);
			$temp['detail'] = $detail;
			$temp['title']  = '修改栏目';
			$this->_showEdit($temp);
			return ;
		}
		$update = $this->_getEditData();
		if( empty($update) ){
			alert('增加栏目失败:数据错误',ADMIN_DIR.'menu/index');
			exit();
		}

		$where = array('id'=>$mid);
		if($this->menu->update($where,$update)){
			alert('修改栏目成功',ADMIN_DIR.'menu/index');
		}else{
			alert('修改栏目失败',ADMIN_DIR.'menu/index');
		}

	}

	public function delete(){
		$mid = $this->input->get('mid',TRUE);
		if($this->menu->deleteByID($mid)){
			alert('删除栏目成功',ADMIN_DIR.'menu/index');
		}else{
			alert('删除栏目失败',ADMIN_DIR.'menu/index');
		}
	}
}
